// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: getApp().globalData.username,
    //username: '18727508188',
    img: "/images/defaultImage.png",
    unreadNum: 1,
    turn: getApp().globalData.autoRemind
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      username: getApp().globalData.username
    })
    this.getInfo();
    this.enterPage(11)
  },

  /**
   * 进入mine页面，通知后端将进入此页面次数加一
   */
  enterPage: function (e) {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'patientsettablebar',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        unitphone: getApp().globalData.username,
        pageNum: e
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      username: getApp().globalData.username
    })
    this.getInfo()
  },

   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo()
  },

  /**
   * 设置个人信息
   */
  selfSetUp: function(){
    wx.navigateTo({
      url: '../setup/setup',
    })
  },

  /**
   * 查看我的通知
   */
  viewMyInfo: function(){
    wx.navigateTo({
      url: '../myInfo/myInfo',
    })
  },

  /**
   * 是否有未读消息
   */
  getInfo: function(){
    var that = this
    console.log(getApp().globalData.username)
    wx.request({
      url:  getApp().globalData.url+'assistantGetSelfInfo',
      method: 'GET',
      data: {
        username: getApp().globalData.username,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        that.setData({
          img: res.data.img,
          unreadNum: res.data.unreadNum,
        })
      }
    })
  },

  /**
   * 设置备忘录
   */
  selfRemind:function(){
    wx.navigateTo({
      url: '../selfRemind/selfRemind',
    })
  },

  /**
   * 久坐提醒
   */
  changeSwitch: function (e) {
    var that = this
    console.log(e)
    getApp().globalData.autoRemind=e.detail.value
    that.setData({
      turn:e.detail.value
    })
    wx.setStorage({
      key: 'autoRemind',
      data: that.data.turn,
    })
    if(that.data.turn==false){
      clearInterval(getApp().globalData.clock)
    }
    else{
      getApp().globalData.clock = setInterval(
        function () {
          if (getApp().globalData.autoRemind == '0' || getApp().globalData.autoRemind == false) return;
          wx.showModal({
            title: '您已经使用该小程序三十分钟',
            content: '站起来，活动活动身子吧!',
            showCancel: false,
            cancelText: "否", //默认是“取消”
            cancelColor: 'skyblue', //取消文字的颜色
            confirmText: "我知道了", //默认是“确定”
            confirmColor: 'skyblue', //确定文字的颜色
            success: function (sm) {
              if (sm.confirm) {
              } else if (sm.cancel) { }
            }
          })
        }, 1800000);
    }
  },

  /**
   * 用户退出登录
   */
  exitLogin: function(){
    wx.showModal({
      title: '退出',
      content: '确定退出吗？',
      success: function(sm) {
        if (sm.confirm) {
          try {
            wx.clearStorageSync()
          } catch (e) {
            // Do something when catch error
          }
          wx.clearStorage()
          wx.redirectTo({
            url: '../login/login',
          })
        }
      }
    })
  },
})